<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuimini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="role_name" lay-verify="required" lay-reqtext="角色名称不能为空" placeholder="请输入角色名称" value="" class="layui-input">
            <tip>填写角色的名称。</tip>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea name="desc" class="layui-textarea" placeholder="请输入备注信息"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">菜单权限</label>
        <div class="layui-input-block">
            <div id="auth_menu" class="demo-tree-more"></div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="/static/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form','tableSelect'], function () {
        var form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            $ = layui.$;

        var data = "<<<.tree_list_json>>>"
        data = JSON.parse(data);
        console.log(data);
        //基本演示
        tree.render({
            elem: '#auth_menu'
            ,data: data
            ,showCheckbox: true  //是否显示复选框
            ,id: 'id'
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var checkData = tree.getChecked('id');

            var menuIds = getChecked_list(checkData);
            var menuIdArr = menuIds.split(',');
            var menu_auth_json = JSON.stringify(menuIdArr);

            data = data.field
            $.ajax({
                url: "/admin/ajax_role_add",
                data:{role_name:data.role_name,desc:data.desc,menu_auth_json:menu_auth_json},
                type:"POST",
                success: function(data){
                    layer.msg(data.msg);
                    if(data.code == 10000){
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);

                        parent.layui.table.reload('currentTableId',{page:{curr:1}}); //demo表格id
                    }

                }
            });

            return false;
        });

        // 获取选中节点的id
        function getChecked_list(data) {
            var id = "";
            $.each(data, function (index, item) {
                if (id != "") {
                    id = id + "," + item.id;
                }
                else {
                    id = item.id;
                }
                var i = getChecked_list(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            });
            return id;
        }

    });
</script>
</body>
</html>